﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FAP.DataAccess.BLL;

namespace FAP.GUI.UC
{
    public partial class ucListThread : UserControl
    {
        //FAP.DataAccess.FAPDataContext context = new FAP.DataAccess.FAPDataContext();
        public ucListThread()
        {
            InitializeComponent();
        }

        bool flag_chb = false;// cho load lại listview
        bool flag_chb1 = false; // cho checkbox all
        bool flag_loadSearch = false;// khi search button khoi chay loadlistview item.checked
        int demItem = 0;
        List<baiviet> lvdisplay = new List<baiviet>();
        List<baiviet> select_thread = new List<baiviet>();
        List<baiviet> lws = new List<baiviet>();

        private void LoadDanhSach(List<baiviet> sw)
        {
            lvdisplay = new List<baiviet>();
            baiviet ws;
            for (int i = 0; i < sw.Count; i++)
            {
                ws = new baiviet();
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Add(sw[i].id.ToString());
                lvi.SubItems.Add(sw[i].tieude);
                lvi.SubItems.Add(sw[i].link);
                lvi.SubItems.Add(sw[i].ngaytao.ToString());
                ws = sw[i];
                lvdisplay.Add(ws);

                lvThread.Items.Add(lvi);
                if (select_thread == null)
                    continue;
                for (int j = 0; j < select_thread.Count; j++)
                {
                    if (sw[i].id == select_thread[j].id)
                    {
                        lvThread.Items[i].Checked = true;
                        break;
                    }
                }
            }
            demItem = 0;
            for (int i = 0; i < lvdisplay.Count; i++)
            {
                if (lvThread.Items[i].Checked == true)
                {
                    demItem++;
                }
            }
        }

        private void DisplaySelection()
        {
            if (chbCheckAll.Checked == true)
            {

                for (int i = lvThread.Items.Count - 1; i >= 0; i--)
                {
                    lvThread.Items[i].Checked = true;
                    lvThread.Items[i].BackColor = System.Drawing.Color.Lavender;

                }
                demItem = lvThread.Items.Count;
                int lvdisplayCount = lvdisplay.Count;
                List<baiviet> lwsTemp = new List<baiviet>(lvdisplay);

                for (int i = 0; i < lvdisplayCount; i++)
                {
                    for (int j = 0; j < select_thread.Count; j++)
                    {
                        if (lwsTemp[i].id == select_thread[j].id)
                        {
                            lwsTemp.RemoveAt(i);
                            i--;
                            lvdisplayCount--;
                            break;
                        }
                    }

                }
                select_thread.AddRange(lwsTemp);
            }
            else
            {
                List<baiviet> temp = new List<baiviet>();


                for (int i = lvThread.Items.Count - 1; i >= 0; i--)
                {
                    lvThread.Items[i].Checked = false;
                    lvThread.Items[i].BackColor = System.Drawing.Color.White;

                }
                demItem = 0;
                for (int i = 0; i < lvdisplay.Count; i++)
                {
                    for (int j = 0; j < select_thread.Count; j++)
                    {
                        if (lvdisplay[i].id == select_thread[j].id)
                        {
                            select_thread.RemoveAt(j);
                            break;
                        }
                    }
                }
            }
        }

        private void LoadLVDanhSachWebsite()
        {
            //chbCheckAll.Checked = false;
            lvdisplay = new List<baiviet>();
            baiviet ws;
            lvThread.Items.Clear();
            string search = txtSearch.Text;
            int dem = -1;

            for (int i = 0; i < lws.Count; i++)
            {
                if (lws[i].tieude != null && (lws[i].tieude.Contains(search) || lws[i].link.Contains(search)))
                {
                    ws = new baiviet();
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Add(lws[i].id.ToString());
                    lvi.SubItems.Add(lws[i].tieude);
                    lvi.SubItems.Add(lws[i].link);
                    lvi.SubItems.Add(lws[i].ngaytao.ToString());
                    ws.id = lws[i].id;
                    ws.link = lws[i].link;
                    ws.tieude = lws[i].tieude;
                    ws.ngaytao = lws[i].ngaytao;
                    lvdisplay.Add(ws);

                    flag_chb = true;
                    lvThread.Items.Add(lvi);
                    dem++;
                    flag_chb = false;

                    for (int j = 0; j < select_thread.Count; j++)
                    {
                        if (lws[i].id == select_thread[j].id)
                        {
                            lvThread.Items[dem].Checked = true;
                            break;
                        }
                    }
                }
            }
            demItem = 0;
            for (int i = 0; i < lvdisplay.Count; i++)
            {
                if (lvThread.Items[i].Checked == true)
                {
                    demItem++;
                }
            }

            int count = lvThread.Items.Count;
            if (demItem == count)
            {
                flag_chb1 = true;
                chbCheckAll.Checked = true;
                flag_chb1 = false;
            }
            else
            {
                flag_chb1 = true;
                chbCheckAll.Checked = false;
                flag_chb1 = false;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {

        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                List<baiviet> sw = baiviet.baivietSelectAll().Where(w => w.isdeleted == false).OrderByDescending(s => s.ngaytao).ToList();
                lws = sw;
                LoadLVDanhSachWebsite();
            }
            catch { };
        }

        private void ucListThread_Load(object sender, EventArgs e)
        {
            try
            {
                var queryAllCustomers = baiviet.baivietSelectAll().Where(w => w.isdeleted == false).OrderByDescending(s => s.ngaytao).ToList();
                List<baiviet> sw = queryAllCustomers;
                lws = sw;
                LoadDanhSach(sw);
                demItem = 0;
            }
            catch { };
        }

        private void chbCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (flag_chb1 == true)
                return;
            flag_chb = true;
            //string search = txtSearch.Text;
            DisplaySelection();
            flag_chb = false;
        }

        private void lvThread_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            baiviet ws = new baiviet();

            if (flag_chb == true)
                return;
            if (flag_loadSearch == true)
                return;
            int j = lvThread.Items.Count;
            if (e.Item.Checked == false)
            {
                if (demItem == j)
                {
                    flag_chb1 = true;
                    chbCheckAll.Checked = false;
                    flag_chb1 = false;
                }
                demItem--;
                e.Item.BackColor = System.Drawing.Color.White;

                for (int i = 0; i < select_thread.Count; i++)
                {
                    if (e.Item.SubItems[1].Text.ToString() == select_thread[i].id.ToString())
                    {

                        select_thread.RemoveAt(i);
                        break;
                    }
                }

            }
            else
            {
                demItem++;
                e.Item.BackColor = System.Drawing.Color.Lavender;
                if (demItem == j)
                {
                    flag_chb1 = true;
                    chbCheckAll.Checked = true;
                    flag_chb1 = false;
                }
                bool flag_selectEdit = false;
                if (select_thread != null)
                {
                    for (int i = 0; i < select_thread.Count; i++)
                    {
                        if (e.Item.SubItems[1].Text.ToString() == select_thread[i].id.ToString())
                        {
                            flag_selectEdit = true;
                        }
                    }
                }
                if (flag_selectEdit == false)
                {
                    int indexItem = e.Item.Index;
                    ws = lvdisplay[indexItem];
                    //ws.id = int.Parse(e.Item.SubItems[1].Text.ToString());
                    //ws.link = e.Item.SubItems[2].Text.ToString();
                    if (select_thread == null)
                        select_thread = new List<baiviet>();
                    select_thread.Add(ws);
                }

            }
        }

        public delegate void SelectedHandler(List<baiviet> sender);
        public event SelectedHandler onSelectedWebSite;

        private void btnSelect_Click_1(object sender, EventArgs e)
        {
            if (onSelectedWebSite != null)
            {
                onSelectedWebSite(select_thread);
            }
        }

        public List<baiviet> GetSelectedThred()
        {
            return select_thread;
        }

    }
}
